/*
 *  $Id: Runta.cs 1051 2010-05-18 22:10:41Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008-2009 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

namespace Ragima.Cards001.Earth.Creatures {
    public class Runta: DemonCreatureCard {
        public Runta(): base("001_e_c_runta") {
            Level = CompleteLevel.ReadyForTest;
            Version = 0;
            Element = ElementType.Earth;
            Value = 100;
            Damage = 3;
            Health = 5;
            AddManaCost(ElementType.Earth, 4);
            AddManaCost(ElementType.Darkness, 1);
            PactValue = 1;
        }

        public override void SetNaturalAffects(ICollector<Affect> affects) {
            affects.Add<RuntaAffect>();
        }

        public override void ExecutePact(Player owner) {
            DoPact(owner);
            DoPact(owner.Enemy);
        }

        private static void DoPact(Player owner) {
            foreach (Creature cr in owner.GetCreatures()) {
                cr.HealBy(new DamageInfo(DamageType.Health, 1));
                cr.AddAffect<RuntaAura>();
            }
        }

        private class RuntaAffect: Affect {
            private bool _hit;

            public RuntaAffect() {
                _hit = false;
            }

            public override string Id {
                get { return AffectManager.GetCardId<Runta>(null); }
            }

            public override void SetAffect() {
                base.SetAffect();
                LocalSummary.OnStartDefencePhase += DoDefence;
                LocalSummary.OnHit += DoHit;
            }
            public override void ResetAffect() {
                base.ResetAffect();
                LocalSummary.OnStartDefencePhase -= DoDefence;
                LocalSummary.OnHit -= DoHit;
            }

            private void DoDefence() {
                Creature cr = Owner as Creature;
                if (cr != null && !cr.Disabled)
                    Owner.AddAffect(new TimeAffect(1, new Affects.Guardian()));
            }

            private void DoHit(Creature hitter, AbstractObject target, int damage) {
                if (_hit) return;
                _hit = true;
                if (hitter.State == CreatureState.Offence && target is Creature) {
                    foreach (Creature cr in target.Owner.GetCreatures()) {
                        if (cr != target && hitter.DistanceTo(cr) <= 2)
                            cr.DamageBy(hitter.NewDamage());
                    }
                }
                _hit = false;
            }
        }

        private class RuntaAura: SpellAffect<Runta> {
            public RuntaAura(): base(true) {
                DefaultDescription = true;
                Add(new Affects.ModDamage(-1));
            }
        }
    }
}
